<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('基金名称')" prop="fundName">
            <el-input v-model="form.fundName" :placeholder="$t('基金名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金简称')" prop="fundAbbreviation">
            <el-input v-model="form.fundAbbreviation" :placeholder="$t('基金简称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('成立时间')" prop="establishTime">
            <el-date-picker
              v-model="form.establishTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('成立时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('备案时间')" prop="recordTime">
            <el-date-picker
              v-model="form.recordTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('备案时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('注册地')" prop="registerPlace">
            <el-input v-model="form.registerPlace" :placeholder="$t('注册地')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金规模') + $t('金额单位')" prop="fundSize">
            <Money v-model="form.fundSize" unit="w" :placeholder="$t('基金规模')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('实缴资本') + $t('金额单位')" prop="paidCapital">
            <Money v-model="form.paidCapital" unit="w" :placeholder="$t('实缴资本')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资领域')" prop="investmentField">
            <el-input v-model="form.investmentField" :placeholder="$t('投资领域')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资阶段')" prop="investmentStage">
            <el-input v-model="form.investmentStage" :placeholder="$t('投资阶段')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资项目数量')" prop="investProNumAmount">
            <el-input v-model="form.investProNumAmount" :placeholder="$t('投资项目数量')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资总金额') + $t('金额单位')" prop="totalInvestmentAmount">
            <Money v-model="form.totalInvestmentAmount" unit="w" :placeholder="$t('投资总金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('退出项目数量')" prop="quitProNumAmount">
            <el-input v-model="form.quitProNumAmount" :placeholder="$t('退出项目数量')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('退出总金额') + $t('金额单位')" prop="totalWithdrawalAmount">
            <Money v-model="form.totalWithdrawalAmount" unit="w" :placeholder="$t('退出总金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('目前持有价值') + $t('金额单位')" prop="currentHoldingValue">
            <Money v-model="form.currentHoldingValue" unit="w" :placeholder="$t('目前持有价值')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('退出方式')" prop="quitMode">
            <el-input v-model="form.quitMode" :placeholder="$t('退出方式')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('IRR')" prop="fundIrr">
            <el-input v-model="form.fundIrr" :placeholder="$t('IRR')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('TVPI')" prop="tvpi">
            <span slot="label">
              {{ $t('TVPI') }}
              <el-tooltip :content="$t('（退出总金额 + 目前持有价值）/ 实缴资本')" placement="top">
                <i class="el-icon-question" />
              </el-tooltip>
            </span>
            <el-input-number v-model="form.tvpi" :placeholder="$t('TVPI')" :controls="false" :precision="4" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('DPI')" prop="dpi">
            <span slot="label">
              {{ $t('DPI') }}
              <el-tooltip :content="$t('退出总金额 / 实缴资本')" placement="top">
                <i class="el-icon-question" />
              </el-tooltip>
            </span>
            <el-input-number v-model="form.dpi" :placeholder="$t('dpi')" :controls="false" :precision="4" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="gp:remanageFund" />
    </div>
  </div>
</template>

<script>
import {
  saveGpRemanageFund,
  getGpRemanageFund,
  calculate
} from '@/api/fund/gp/remanageFund/gpRemanageFund'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  fundName: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  fundAbbreviation: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  establishTime: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  registerPlace: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  fundSize: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  paidCapital: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  investmentField: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  investmentStage: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  investProNumAmount: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  totalInvestmentAmount: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  quitProNumAmount: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  totalWithdrawalAmount: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  currentHoldingValue: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  quitMode: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  fundIrr: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }]
                }
            }
        },
        watch: {
          'form.paidCapital': {
            handler(newValue) {
              if (newValue === '' || newValue === undefined) {
                this.form.paidCapital = 0
              }
              this.calculate(this.form)
            },
            immediate: true
          },
          'form.totalWithdrawalAmount': {
            handler(newValue) {
              if (newValue === '' || newValue === undefined) {
                this.form.totalWithdrawalAmount = 0
              }
              this.calculate(this.form)
            },
            immediate: true
          },
          'form.currentHoldingValue': {
            handler(newValue) {
              if (newValue === '' || newValue === undefined) {
                this.form.currentHoldingValue = 0
              }
              this.calculate(this.form)
            },
            immediate: true
          }

        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                if (data != null && data.id !== undefined) {
                    getGpRemanageFund(data.id).then(response => {
                        this.form = response.data
                    })
                } else {
                    this.reset()
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    gpId: this.$route.query.gpId,
                    fundName: undefined,
                    fundAbbreviation: undefined,
                    establishTime: undefined,
                    recordTime: undefined,
                    registerPlace: undefined,
                    fundSize: 0,
                    paidCapital: 0,
                    investmentField: undefined,
                    investmentStage: undefined,
                    investProNumAmount: undefined,
                    totalInvestmentAmount: 0,
                    quitProNumAmount: undefined,
                    totalWithdrawalAmount: 0,
                    currentHoldingValue: 0,
                    quitMode: undefined,
                    tvpi: 0,
                    dpi: 0,
                    fundIrr: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveGpRemanageFund(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          // 计算TVPI与DPI
          calculate(data) {
            calculate(data).then(response => {
              this.form.tvpi = response.data.tvpi
              this.form.dpi = response.data.dpi
            })
          }

        }

    }
</script>
